<ul class="treeview-gray treeview pipeline" id="pipeline_config_tree">
    <li class="collapsable">
        <div class="hitarea collapsable-hitarea"></div>
        <% is_pipeline_parent = !params[:stage_name].nil? %>
        <% config_type_edit_path = params[:stage_parent] == "pipelines" ? pipeline_edit_path(:pipeline_name =>scope[:pipeline].name(), :current_tab => "general") : template_edit_path(:pipeline_name => scope[:pipeline].name(), :stage_parent => "templates", :current_tab => 'general') %>
        <%= link_to(scope[:pipeline].name(), config_type_edit_path, :class => is_pipeline_parent ? "parent_selected" : "selected", :title => scope[:pipeline].name()) -%>
        <% if params[:stage_parent] == "pipelines" && scope[:pipeline].hasTemplate() %>
            <ul class="template">
                <li title="<%= scope[:pipeline].getTemplateName() -%>">
	                <div class="template_name">
                    <% if is_user_an_admin? || is_user_a_template_admin_for_template?(scope[:pipeline].getTemplateName)%>
                        <%= link_to(scope[:pipeline].getTemplateName(), template_edit_path(:pipeline_name => scope[:pipeline].getTemplateName(), :stage_parent => "templates", :current_tab => 'general'), :class => "edit_template") -%>
                    <% else %>
                        <%= scope[:pipeline].getTemplateName() -%>
                    <% end %>
                    <% if is_user_authorized_view_template?(scope[:pipeline].getTemplateName) %>
                        <%= link_to('', "#{scope[:pipeline].getTemplateName()}", :class => "preview_icon_wobg action_icon view_template_tree_link skip_dirty_stop", :title => "Preview") -%>
                    <% end %>
					</div>
                </li>
            </ul>
        <% else %>
            <ul class="stages">
                <% scope[:pipeline].each do |stage| %>
                    <% current_stage = stage.name().to_s === params[:stage_name]
                       is_stage_parent = current_stage && !params[:job_name].nil?
                       stage_class = current_stage ? "collapsable" : "expandable"
                    %>
                    <li class="<%= stage_class -%>">
                        <div class="hitarea expandable-hitarea"></div>
                        <%= link_to(stage.name(), tab_aware_stage_link(params[:stage_parent], scope[:pipeline].name(), stage.name(), params[:current_tab]), :class => is_stage_parent ? "parent_selected" : current_stage ? "selected" : "", :title => stage.name()) -%>
                        <ul class="jobs<%= " hidden" unless current_stage %>">
                            <% stage.getJobs().each do |job| %>
                                <li><%= link_to(job.name(), tab_aware_job_link(params[:stage_parent], scope[:pipeline].name(), stage.name(), job.name(), params[:current_tab]), :class => current_stage && job.name().to_s == params[:job_name] ? "selected" : "", :title => job.name()) -%></li>
                            <% end %>
                        </ul>
                    </li>
                <% end %>
            </ul>
        <% end %>
    </li>
</ul>

<script type="text/javascript">
    Util.on_load(function() {
        jQuery('#pipeline_config_tree').treeview({
            'persist' : "location"
        });
      jQuery('a.view_template_tree_link').click(function() {
        const templateName = jQuery('a.view_template_tree_link').attr('href');
        RenderTemplateInModal(templateName);
        return false;
      });
    });
</script>
